
package com.example.dynamic_form.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dynamic_form.domain.po.ModelPo;
import com.example.dynamic_form.service.IModelService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("model")
public class ModelController {

    @Resource
    private IModelService modelService;

    @GetMapping("listModel")
    public List<ModelPo> listUser() {
        return modelService.list();
    }

    @PostMapping("createModel")
    public ModelPo createForm(@RequestBody ModelPo modelPo) {
        modelService.save(modelPo);
        return modelPo;
    }

    @GetMapping("getModelByModule")
    public HashMap getModelByModule(@RequestParam String module) {
        HashMap map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("module", module);
        ModelPo model = modelService.getOne(queryWrapper);
        map.put("code", 200);
        map.put("data", model);
        return map;
    }

    @PostMapping("updateModel")
    public HashMap updateModelByModule(@RequestBody ModelPo modelPo) {
        HashMap map = new HashMap<>();
        boolean res = modelService.saveOrUpdate(modelPo);
        if (res) {
            map.put("code", 200);
            map.put("data", true);
        } else {
            map.put("code", 400);
            map.put("data", false);
        }
        return map;
    }

}
